$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/weixininfo/list',
        datatype: "json",
        colModel: [			
			{ label: '公众号ID', name: 'id', index: 'id',align: "center", width: 50, key: true,formatter:function(value,options,rowData){
				var weixinName=rowData["weixinName"];
				if(1==1){
					return "<a onclick='update("+value+")'>"+weixinName+"</a>";
					}
				}
			},
			// { label: '公众号ID', name: 'weixinName', index: 'weixin_name',align: "center", width: 80 ,formatter:function(value,options,rowData){
			// 		var id=rowData["id"];
			// 		return "<a onclick='update("+id+")'>"+value+"</a>";
			// 	}
			// },
			{ label: '公众号', name: 'weixinNikeName', index: 'weixin_nike_name',align: "center", width: 80},
			{ label: '品牌', name: 'brandName', index: 'brand_name',align: "center", width: 80 },
			{ label: '删除段落', name: 'deleteParagraphsList', index: 'delete_paragraphs_list',align: "center", width: 80 },
			{ label: '是否品牌公众号', name: 'isOfficial', index: 'is_official',align: "center", width: 80,formatter:function(value,options,rowData){
					if(value==0){
						return '否';
					}else{
						return '是';
					}
				}
			},
			{ label: '热度', name: 'articleHotScore', index: 'article_hot_score',align: "center", width: 80 },
			{ label: '广告关键字', name: 'adKeyword', index: 'ad_keyword',align: "center", width: 80 },
			{ label: '备注', name: 'remark', index: 'remark',align: "center", width: 80 },
		],
		viewrecords: true,
		height: '100%',
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
		postData:{"brandName":vm.q.brandName,"weixinName":vm.q.weixinName,"weixinNikeName":vm.q.weixinNikeName},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
setTimeout(function () {
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			console.log(r);
			if(r.code==0){
				for(var i=0;i<r.brandInfo.length;i++){
					$("#brandName").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
					$("#brand").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
				$("#brandNames").scrollTop()
			}
		}
	});
},2000)

function update(e) {
	vm.showList = false;
	vm.title = "修改";

	vm.getInfo(e);
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		weixinInfo: {},
		q:{
			brandName:"",
			weixinName:"",
			weixinNikeName:""
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.weixinInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			vm.weixinInfo.isOfficial=0;
			if($('#checked').prop('checked')){
				vm.weixinInfo.isOfficial=1;
			}
			if(vm.weixinInfo.weixinName==null||vm.weixinInfo.weixinName==""||vm.weixinInfo.weixinNikeName==null||vm.weixinInfo.weixinNikeName==""||vm.weixinInfo.brandName==null||vm.weixinInfo.brandName==""){
				alert("请填写完整信息");
				return;
			}
			var url = vm.weixinInfo.id == null ? "sys/weixininfo/save" : "sys/weixininfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.weixinInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.back();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/weixininfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/weixininfo/info/"+id, function(r){
                vm.weixinInfo = r.weixinInfo;
				console.log(JSON.stringify(vm.weixinInfo))
				if(vm.weixinInfo.isOfficial==1){
					$("#checked").prop("checked",true);
				}else{
					$("#checked").prop("checked",false);
				}
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = 1;
			vm.q.brandName=$("#brands").val();
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				postData:{"brandName":vm.q.brandName,"weixinName":vm.q.weixinName,"weixinNikeName":vm.q.weixinNikeName},
            }).trigger("reloadGrid");
		},
		back: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			vm.q.brandName=$("#brands").val();
			$("#jqGrid").jqGrid('setGridParam',{
				page:page,
				postData:{"brandName":vm.q.brandName,"weixinName":vm.q.weixinName,"weixinNikeName":vm.q.weixinNikeName},
			}).trigger("reloadGrid");
		}
	}
});